package com.fancon.android.gardenjokesx.adapter;

import java.net.URLDecoder;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fancon.android.emoji.widget.RichTextView;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.activity.M042Activity;
import com.fancon.android.gardenjokesx.entity.User;
import com.fancon.android.gardenjokesx.utils.Constant;
import com.fancon.android.gardenjokesx.utils.StringUtil;
import com.fancon.android.gardenjokesx.utils.UserUtil;
import com.fancon.android.ui.widget.ImageCacheView;

public class ListUserTitleAdapter extends BaseAdapter{
	private ArrayList<User> mListUserTitle;
	private Activity mContext;
	private LayoutInflater inflater;
	private int mScreenId;
	private String currentUserName;
	public ListUserTitleAdapter(Activity context,ArrayList<User> listUserTitle,int screenId) {
		mListUserTitle = listUserTitle;
		mContext = context;
		mScreenId = screenId;
		currentUserName = UserUtil.getInfoUserLogin(context).getUser_name();
		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// TODO Auto-generated constructor stub
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListUserTitle.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convetView, ViewGroup arg2) {
		View vi = convetView;
		final ViewHolder holder;
		if(vi == null){
			vi = inflater.inflate(R.layout.item_list_user_title, null);
			holder = new ViewHolder();
			holder.imgAvatar = (ImageCacheView) vi.findViewById(R.id.t03_img_friend_avatar);
			holder.tvName = (RichTextView) vi.findViewById(R.id.t03_tv_Name);
			holder.tvUserName = (TextView) vi.findViewById(R.id.t03_tv_UserName);
			holder.llProfileInfo = (LinearLayout) vi.findViewById(R.id.t03_profile_info);
			holder.btnFollow = (Button) vi.findViewById(R.id.t03_btn_follow);
			holder.btnAbout = (Button) vi.findViewById(R.id.t03_btn_about);
			holder.btnAccept = (Button) vi.findViewById(R.id.t03_btn_accept);
			holder.btnIgnore = (Button) vi.findViewById(R.id.t03_btn_ignore);
			holder.btnRequest = (Button) vi.findViewById(R.id.t03_btn_request);
			vi.setTag(holder);
		}else {
			holder =(ViewHolder) vi.getTag();
		}
		User user = mListUserTitle.get(position);
		holder.imgAvatar.loadImage(user.getUrl_image());
		
		//Check Name - If not Name then Name = UserName
		if(user.getFirst_name().length()==0 && user.getLast_name().length()==0){
			String str = StringUtil.validString(user.getUser_name(), 12);
			holder.tvName.setText(str);
		} else {
			String str = URLDecoder.decode(user.getFirst_name())+" "+URLDecoder.decode(user.getLast_name());
			str = StringUtil.validString(str, 12);
			holder.tvName.setText(str);
		}
		
		switch (mScreenId) {
		case Constant.U11:
			vi.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					//@binhbt-comment
//					 Intent intent = new Intent();
//					 intent.setClass(((Activity)mContext).getParent(),
//					 M04Activity.class);
//					 String viewedUserId = mListUserTitle.get(position).getUser_id();
//					 intent.putExtra(Constant.USER_ID_VIEW, viewedUserId);
//					 MeshGroupActivity activityStack = (MeshGroupActivity) ((Activity)mContext).getParent();
//					 activityStack.push("thanhndM04", intent);
				}
			});
			holder.btnAbout.setVisibility(View.GONE);
			holder.btnAccept.setVisibility(View.VISIBLE);
			holder.btnIgnore.setVisibility(View.VISIBLE);
			holder.btnIgnore.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					//@binhbt-comment
//					User u = mListUserTitle.get(position);
//					String followingUserId = u.getUser_id();
//					((U11Activity)mContext).confirmRequestFollow(position, followingUserId, "1");
				}
			});
			holder.btnAccept.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					//@binhbt-comment
//					User u = mListUserTitle.get(position);
//					String followingUserId = u.getUser_id();
//					((U11Activity)mContext).confirmRequestFollow(position, followingUserId, "0");
				}
			});
			break;
		case Constant.T03:
			//@binhbt-comment
//			if(user.getAbout().equals("M")){
//				holder.btnAbout.setBackgroundResource(R.drawable.common_title_icon_master);
//			}else {
//				holder.btnAbout.setBackgroundResource(R.drawable.common_title_icon_vanguard);
//			}
			if (user.isIs_blocked()||currentUserName.equals(user.getUser_name())) {
				// Không hiển thị gì cả
			} else {
				if (user.isIs_request()&&user.isIs_private()) {
					// Hiển thị request Me
					holder.btnRequest.setVisibility(View.VISIBLE);
					holder.btnRequest.setText(mContext.getString(R.string.common_waiting));
				} else {
					if(user.isIs_request()){
						holder.btnAccept.setVisibility(View.VISIBLE);
						holder.btnIgnore.setVisibility(View.VISIBLE);
					}
					if (user.isIs_private() && !user.isIs_following()) {
						holder.btnRequest.setVisibility(View.VISIBLE);
						if(user.isIs_waitting()){
							holder.btnRequest.setText(mContext.getString(R.string.common_waiting));
						}else {
							holder.btnRequest.setText(mContext.getString(R.string.t03_btn_request));
						}
					} else {
						holder.btnFollow.setVisibility(View.VISIBLE);
						if (user.isIs_following()) {
							holder.btnFollow.setText(mContext.getString(R.string.common_btn_following));
							holder.btnFollow.setBackgroundResource(R.drawable.common_btn_follow_active);
						} else {
							holder.btnFollow.setText(mContext.getString(R.string.common_btn_follow));
							holder.btnFollow.setBackgroundResource(R.drawable.common_btn_follow);
						}
					}
				}  
			}
			holder.btnAbout.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Log.d("About", "OK");
				}
			});
			holder.btnFollow.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
//					User u = mListUserTitle.get(position);
//					String followingUserId = u.getUser_id();
//					String isFollowing;
//					if(u.isIs_following()){
//						isFollowing = "false";
//					}else {
//						isFollowing = "true";
//					}
					//@binhbt-comment
//					Log.d("UserId", followingUserId);
//					((T03Activity)mContext).regFollow(position, followingUserId, isFollowing);
				}
			});
			holder.llProfileInfo.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					//@binhbt-comment
//					 Intent intent = new Intent();
//					 intent.setClass(((Activity)mContext).getParent(),
//					 M04Activity.class);
//					 String viewedUserId = mListUserTitle.get(position).getUser_id();
//					 intent.putExtra(Constant.USER_ID_VIEW, viewedUserId);
//					 MeshGroupActivity activityStack = (MeshGroupActivity) ((Activity)mContext).getParent();
//					 activityStack.push("thanhndM04", intent);
				}
			});
			break;
		case Constant.M042:
			holder.btnAbout.setVisibility(View.GONE);
			if (user.isIs_blocked()||currentUserName.equals(user.getUser_name())) {
				// Không hiển thị gì cả
			} else {
				if (user.isIs_request()&&user.isIs_private()) {
					// Hiển thị request Me
					holder.btnRequest.setVisibility(View.VISIBLE);
					holder.btnRequest.setText(mContext.getString(R.string.common_waiting));
				} else {
					if(user.isIs_request()){
						holder.btnAccept.setVisibility(View.VISIBLE);
						holder.btnIgnore.setVisibility(View.VISIBLE);
					}
					if (user.isIs_private() && !user.isIs_following()) {
						holder.btnRequest.setVisibility(View.VISIBLE);
						if(user.isIs_waitting()){
							holder.btnRequest.setText(mContext.getString(R.string.common_waiting));
						}else {
							holder.btnRequest.setText(mContext.getString(R.string.t03_btn_request));
						}
					} else {
						holder.btnFollow.setVisibility(View.VISIBLE);
						if (user.isIs_following()) {
							holder.btnFollow.setText(mContext.getString(R.string.common_btn_following));
							holder.btnFollow.setBackgroundResource(R.drawable.common_btn_follow_active);
						} else {
							holder.btnFollow.setText(mContext.getString(R.string.common_btn_follow));
							holder.btnFollow.setBackgroundResource(R.drawable.common_btn_follow);
						}
					}
				}  
			}
			holder.btnRequest.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					User u = mListUserTitle.get(position);
					String followingUserId = u.getUser_id();
					String isFollowing;
					if(u.isIs_following()){
						isFollowing = "false";
					}else {
						isFollowing = "true";
					}
					Log.d("UserId", followingUserId);
					((M042Activity)mContext).regFollow(position, followingUserId, isFollowing);
					holder.btnRequest.setText(mContext.getString(R.string.common_waiting));
				}
			});
			holder.btnFollow.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					User u = mListUserTitle.get(position);
					String followingUserId = u.getUser_id();
					String isFollowing;
					if(u.isIs_following()){
						isFollowing = "false";
					}else {
						isFollowing = "true";
					}
					Log.d("UserId", followingUserId);
					((M042Activity)mContext).regFollow(position, followingUserId, isFollowing);
				}
			});
			holder.llProfileInfo.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					//@binhbt-comment
//					 Intent intent = new Intent();
//					 String viewedUserId = mListUserTitle.get(position).getUser_id();
//					 if(mContext.getParent() != null){
//					 intent.setClass(mContext.getParent(),M04Activity.class);
//					 intent.putExtra(Constant.USER_ID_VIEW, viewedUserId);
//					 MeshGroupActivity activityStack = (MeshGroupActivity) (mContext).getParent();
//					 activityStack.push("thanhndM04", intent);
//					 } else {
//						 intent.setClass(mContext, M04Activity.class);
//						 intent.putExtra(Constant.USER_ID_VIEW, viewedUserId);
//						 mContext.startActivity(intent);
//					 }
				}
			});
			holder.btnIgnore.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					User u = mListUserTitle.get(position);
					String followingUserId = u.getUser_id();
					((M042Activity)mContext).confirmRequestFollow(position, followingUserId, "1");
				}
			});
			holder.btnAccept.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					User u = mListUserTitle.get(position);
					String followingUserId = u.getUser_id();
					((M042Activity)mContext).confirmRequestFollow(position, followingUserId, "0");
				}
			});
			break;
		default:
			break;
		}
		String username= StringUtil.validString(user.getUser_name(), 7);
		holder.tvUserName.setText(username);
		// TODO Auto-generated method stub
		return vi;
	}
	
	static class ViewHolder{
		Button btnAccept,btnIgnore,btnRequest,btnCancelRequest,btnFollow,btnAbout;
		LinearLayout llProfileInfo;
		ImageCacheView imgAvatar;
		TextView tvUserName,tvName;
		
	}
}
